﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F5LocalTroubles : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F5LocalTroubles()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F5LocalTroubles.DoubleClick += F5LocalTroubles_DoubleClick;
            this.F5LocalTroubles.PopupMenuShowing += F5LocalTroubles_PopupMenuShowing;
            this.f5F5LocalTroublesContextMenu.ItemClicked += f5LocalTroublesContextMenu_ItemClicked;
        }

        void f5LocalTroublesContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f5F5LocalTroublesContextMenu.Hide();
            DataRow row = F5LocalTroubles.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    frm_Edit_F5LocalTroubles editForm = new frm_Edit_F5LocalTroubles();
                    editForm.troubleID = Int32.Parse(row[0].ToString());
                    if (editForm.LoadData())
                    {
                        editForm.Show();
                    }
            
                    break;
                case "addSelection":
                    frm_Add_F5LocalTroubles add_Form = new frm_Add_F5LocalTroubles();
                    add_Form.Show();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F5LocalTroubles_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f5F5LocalTroublesContextMenu.Show(MousePosition);
        }

        private void F5LocalTroubles_DoubleClick(object sender, EventArgs e)
        {            
            DataRow row = F5LocalTroubles.GetFocusedDataRow();
            frm_Edit_F5LocalTroubles editForm = new frm_Edit_F5LocalTroubles();
            editForm.troubleID = Int32.Parse(row[0].ToString());
            if (editForm.LoadData())
            {
                editForm.Show();
            }                        
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [231_F5Troubles].ID, [231_F5Troubles].Date, [231_F5Troubles].OrderNo,"
                + " [231_F5Plan].PartNo, [222_Maker].MakerName, [231_F5Troubles].SerialNo,"
                + " [231_F5Troubles].Quantity, [222_Staff].StaffName, [222_Staff].GroupID,"
                + " [231_F5Troubles].F5TroubleContent, [231_F5Troubles].Reason, [231_F5Troubles].Solution, [231_F5Troubles].Remark"
                + " FROM [231_F5Troubles] LEFT OUTER JOIN"
                + " [231_F5Plan] ON [231_F5Plan].OrderNo=[231_F5Troubles].OrderNo LEFT OUTER JOIN"
                + " [222_Part] ON [231_F5Plan].PartNo=[222_Part].PartNo LEFT OUTER JOIN"
                + " [222_Maker] ON [222_Part].MakerID=[222_Maker].MakerID LEFT OUTER JOIN"
                + " [222_Staff] ON [222_Staff].StaffID=[231_F5Troubles].Checker"
                + " WHERE [231_F5Troubles].Type=0"
                + " ORDER BY [231_F5Troubles].Date";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF5LocalTroubles.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("Product", typeof(string));
                newTable.Columns.Add("Maker", typeof(string));
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("Checker", typeof(string));
                newTable.Columns.Add("GroupID", typeof(string));
                newTable.Columns.Add("F5TroubleContent", typeof(string));                
                newTable.Columns.Add("Reason", typeof(string));                
                newTable.Columns.Add("Solution", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                gridF5LocalTroubles.DataSource = newTable;
            }
            F5LocalTroubles.Columns[0].Caption = "ID";
            F5LocalTroubles.Columns[1].Caption = "Date";
            F5LocalTroubles.Columns[2].Caption = "Order No.";
            F5LocalTroubles.Columns[3].Caption = "Product";
            F5LocalTroubles.Columns[4].Caption = "Maker";
            F5LocalTroubles.Columns[5].Caption = "Serial No.";
            F5LocalTroubles.Columns[6].Caption = "Quantity";
            F5LocalTroubles.Columns[7].Caption = "Checker";
            F5LocalTroubles.Columns[8].Caption = "Group";
            F5LocalTroubles.Columns[9].Caption = "Trouble Content";
            F5LocalTroubles.Columns[10].Caption = "Reason";
            F5LocalTroubles.Columns[11].Caption = "Solution";
            F5LocalTroubles.Columns[12].Caption = "Remark";
            F5LocalTroubles.Columns[0].Visible = false;
            F5LocalTroubles.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F5LocalTroubles add_Form = new frm_Add_F5LocalTroubles();
            add_Form.Show();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            DataRow row = F5LocalTroubles.GetFocusedDataRow();
            frm_Edit_F5LocalTroubles editForm = new frm_Edit_F5LocalTroubles();
            editForm.troubleID = Int32.Parse(row[0].ToString());
            if (editForm.LoadData())
            {
                editForm.Show();
            }
        }
    }
}
